<?php
class Admin_CountryController extends Apps_Controller_Action_Admin {
	//LIST
	public function indexAction() {
		$this->actResolve('act_delete',
				'delete', null, null, array('id' => $this->_getParam('item_checker')),
				true);

		$tbCountry = new M_DbTable_Country();
		$grid = new Apps_Plugin_Grid('countryGrid');

		$grid->setData($tbCountry->getList())
			 ->setTableCaption('List Country')
			 ->setTableColumnFormat(array(
			 		array('title'		=> 'ID',
			 			  'dataField'	=> 'id',
			 			  'width'		=> '5%',
			 			  'class'		=> 'center'),
			 		array('title'		=> 'Name (En)',
			 			  'dataField'	=> 'en_name',
			 			  'class'		=> 'center'),
			 		array('title'		=> 'Name (Vi)',
			 			  'dataField'	=> 'name',
			 			  'class'		=> 'center'),
			 		array('title'		=> 'Description',
			 			  'dataField'	=> 'description',
			 			  'class'		=> 'center'),
			 		array('title'		=> 'Sorting number',
			 			  'dataField'	=> 'sorting_number',
			 			  'class'		=> 'center'),
			 		array('title'		=> 'Created Date',
			 			  'dataField'	=> 'created_date',
			 			  'class'		=> 'center'),
			 		array('title'      => 'Edit',
			 			  'html'       => array('<a href="/admin/country/edit?id=%s"><i class="icon-edit" title="Sửa"></i></a>',
			 			  				  array('id')),
			 			  'width'      => '2%'),
			 		array('title'      => 'Delete',
			 			  'html'       => array('<a href="/admin/country/delete?id=%s"><i class="icon-remove must-confirm " title="Xóa"></i></a>',
			 			  				  array('id')),
			 			  'width'      => '2%')
			 		));
		$this->view->grid = $grid;
	}
	//ADD
	public function addAction() {
		$rq = $this->_request;

		$tbCountry = new M_DbTable_Country();
		$f 	= new Admin_Form_AddCountry();

		if ($rq->isPost()) {
			if ($f->isValid($rq->getParams())) {
				$this->_flashMessenger->addMessage(
				    $tbCountry->safelyInsert($f->getValues())
				    ? self::$msgAddSuccess
				    : self::$msgAddFailed
				);
				$this->_helper->redirector('index', null, null);
			}
		}
		$this->view->form = $f;
	}

	//EDIT
	public function editAction() {
		$rq = $this->_request;
		$id = $rq->getParam('id');

		$tbCountry = new M_DbTable_Country();

		if(!$id || !is_numeric($id) || !$tbCountry->fetchRow(array('id =?' => $id))) {
			$this->_helper->redirector('index', null, null);
		}

		$f 	= new Admin_Form_AddCountry();
		if ($rq->isPost()) {
			if ($f->isValid($rq->getParams())) {
				$this->_flashMessenger->addMessage(
				    $tbCountry->safelyUpdate($f->getValues(), array('id =?' => $id))
				    ? self::$msgUpdateSuccess
				    : self::$msgUpdateFailed
				);
				$this->_helper->redirector('index', null, null);
			}
		}

		$f->setLegend('Edit Country');
	    $f->populate($tbCountry->fetchRow(array('id =?' => $id))->toArray());
		$this->view->form = $f;
		$this->render('add');
	}

	//DELETE
	public function deleteAction() {
		$this->disableLayout(true, true);

		$id = $this->_getParam('id');
		if (!$id) {
		    //nếu là id == 1 : quốc gia là Việt Nam,Lào,Campuchia k xóa: mặc định thêm vào
			$this->_helper->redirector('index', null, null);
		}

		$tbCountry = new M_DbTable_Country();
		if (is_array($id)) {
			foreach ($id as $k => $v) {
				if (is_numeric($v) && !in_array($v, array(1, 2, 3))) {
					$tbCountry->delete(array('id =?'  => $v));
				}
			}
		} else if (is_numeric($id) && !in_array($id, array(1, 2, 3))) {
				if ($tbCountry->delete(array('id =?'  => $id)))
				{
					$this->_flashMessenger->addMessage(self::$msgDeleteSuccess);
				}
		}
		$this->_helper->redirector('index', null, null);
	}
}